package com.yicong.ants.ui.me;

import android.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.cchao.simplelib.core.RxBus;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.interfaces.BaseView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.ActivityCancellationNextBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.ConstantCodeUtils;
import com.yicong.ants.utils.ToastUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * 注销账号
 */
public class CancellationNextActivity extends BaseTitleBarActivity<ActivityCancellationNextBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.activity_cancellation_next;
    }

    @Override
    protected void initEventAndData() {
        AppHelper.transStatus(getWindow());
        setTitleText("注销账号");
        setTitleBarVisible(false);

        mDataBind.button.setEnabled(false);
        mDataBind.code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence != null && charSequence.length() >= 6) {
                    mDataBind.button.setEnabled(true);
                    mDataBind.button.setClickable(true);
                } else {
                    mDataBind.button.setEnabled(false);
                    mDataBind.button.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mDataBind.setClick(this);
        mDataBind.phoneNumber.setText(UserManager.getUserBean().getMobile_mix());
    }

    @Override
    protected void onLoadData() {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back) {
            onBackPressed();
        } else if (id == R.id.get_code) {
            sendVerifyCode(this, this::countDownVerifyCode);
        } else if (id == R.id.button) {
            String code = mDataBind.code.getText().toString();
            if (code.length() == 0) {
                ToastUtils.showLong(this, "请输入验证码");
                return;
            }
            new AlertDialog.Builder(thisActivity)
                    .setTitle("注销账号")
                    .setMessage("您确定要注销此账号吗？")
                    .setPositiveButton("确定", (dialogInterface2, i2) -> {
                        showProgress();
                        Map<String, String> map = new HashMap<>();
                        map.put("code", code);
                        addSubscribe(RetrofitHelper.getApis().destroyAccount(map)
                                .compose(RxHelper.toMain())
                                .subscribe(respBean -> {
                                    hideProgress();
                                    if (respBean.isCodeFail()) {
                                        showToast(respBean.getMsg());
                                        return;
                                    }
                                    logout();
                                }, RxHelper.getHideProgressConsumer(this)));
                    })
                    .setNegativeButton("取消", null)
                    .show();
        }
    }

    private void sendVerifyCode(BaseView baseView, Runnable runnable) {
        Disposable disposable = RetrofitHelper.getApis().sendDestroyVerifyCode(Collections.emptyMap())
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    baseView.hideProgress();
                    baseView.showToast(respBean.getMsg());
                    if (respBean.isCodeFail()) {
                        return;
                    }
                    runnable.run();
                }, RxHelper.getHideProgressConsumer(baseView));
    }

    void countDownVerifyCode() {
        mDataBind.getCode.setClickable(false);
        RxHelper.countDownConsumer(60, intValue -> mDataBind.getCode.setText((60 - intValue) + " s后重发"), new Runnable() {
            @Override
            public void run() {
                mDataBind.getCode.setClickable(true);
                mDataBind.getCode.setText("获取验证码");
            }
        });
    }

    private void logout() {
        UserManager.logout();
        RxBus.get().postEvent(Constants.Event.Login_Out);
        setResult(ConstantCodeUtils.LOGGED_OUT);
        finish();
    }

}
